package com.hmyd.hmyd_android.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.ViewGroup;

/**
 * 自动缩放图片
 * 
 **/
public class CustomImageView extends android.widget.ImageView {

	private int mWidth;
	private int mHeight;

	public CustomImageView(Context context) {
		super(context);
	}
	
	
	public CustomImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public CustomImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}
	
	
	/** 设置裁剪区域 */
	public void setClipSize(int mWidth, int mHeight) {
		if (mWidth <= 0 && mHeight <= 0)
			return;
		this.mWidth = mWidth;
		this.mHeight = mHeight;
		ViewGroup.LayoutParams mLayoutParams = getLayoutParams();
		mLayoutParams.width = mWidth;
		mLayoutParams.height = mHeight;
		setLayoutParams(mLayoutParams);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		Drawable d = getDrawable();

		if (mWidth <= 0 && mHeight <= 0 && d != null) {
			// ceil not round - avoid thin vertical gaps along the left/right
			// edges
			int width = MeasureSpec.getSize(widthMeasureSpec);
			int height = (int) Math.ceil((float) width
					* (float) d.getIntrinsicHeight()
					/ (float) d.getIntrinsicWidth());
			setMeasuredDimension(width, height);
		} else {
			super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		}
	}
}
